package com.dhecp.project.finance.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.finance.domain.AccountCountForCompany;
import com.dhecp.project.finance.domain.AccountDetailedForCompany;
import com.dhecp.project.finance.mapper.AccountCountForCompanyMapper;
import com.dhecp.project.finance.mapper.AccountDetailedForCompanyMapper;
import com.dhecp.project.finance.service.AccountDetailedForCompanyService;
import com.dhecp.project.hr.domain.OperateCompanyApply;
import com.dhecp.project.hr.mapper.OperateCompanyApplyMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 公司账户流水 服务层
 * @author DH-Xxj
 * @version 2022年10月11日
 */
@Service
public class AccountDetailedForCompanyServiceImpl extends BaseModuleServiceImpl implements AccountDetailedForCompanyService {

	@Autowired
	private AccountDetailedForCompanyMapper businessMapper;
	@Autowired
	private AccountCountForCompanyMapper accountCountForCompanyMapper;
	@Autowired
	private OperateCompanyApplyMapper operateCompanyApplyMapper;
	
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 查询
	 * @param params
	 * @return List - AccountDetailedForCompany
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public List<AccountDetailedForCompany> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
		if (params.get("checkwsx") != null && params.get("checkwsx").equals("1")){
			params.put("isTwoDay",1);
			params.remove("checkwsx");
		}
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "t1.createTime desc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}
        return businessMapper.selectAll(params);
    }
	
	/**
	 * 查询汇总金额
	 * @param params
	 * @return List - AccountDetailedForCompany
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public List<AccountDetailedForCompany> queryCountList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "vGsBm asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}
        return businessMapper.selectCountAll(params);
    }

    /**
     * 查询指定公司的汇总金额
     * @param vGsBm 必要参数：公司编码
     * @return AccountDetailedForCompany 实体
     * @throws Exception
     * @author DH-Xxj
     */
	@Override
    public AccountDetailedForCompany queryCountByVGsBm(Map<String, Object> params) throws Exception {
        if (params.get("vGsBm") == null || "".equals(params.get("vGsBm"))){
        	throw new RuntimeException(MessageUtils.message("module.error.lack","公司编码【vGsBm】"));
        }
        return businessMapper.selectCountByVGsBm(params.get("vGsBm").toString());
    }

	@Override
	public void insertAccountDetailedForCompany(String vGsBm, String vGsBmP, BigDecimal fJe, String vJyLx, String vBz,
			String mName, Integer mType, String mNid, String mBid, String mGuid,
			String operatorDept, String operatorPosition, String operatorName, String operatorUserId) throws Exception {
		OperateCompanyApply operateCompanyApply = operateCompanyApplyMapper.findVGsBm(vGsBm);
		OperateCompanyApply operateCompanyApplyP = operateCompanyApplyMapper.findVGsBm(vGsBmP);
		
		AccountCountForCompany accountCountForCompany = accountCountForCompanyMapper.findByVGsBm(vGsBmP);
		Date now = new Date();
		AccountDetailedForCompany entity = new AccountDetailedForCompany();
		entity.setGuid(RandomValueUtils.getGUID());
		entity.setvGsBm(vGsBm);
		entity.setvGsJc(operateCompanyApply.getvGsJc());
		entity.setvGsMc(operateCompanyApply.getvGsMc());
		entity.setvYhZh(operateCompanyApply.getvYhZh());
		entity.setvGsBmP(vGsBmP);
		entity.setvGsJcP(operateCompanyApplyP.getvGsJc());
		entity.setvGsMcP(operateCompanyApplyP.getvGsMc());
		entity.setvYhZhP(operateCompanyApplyP.getvYhZh());
		entity.setvJyLx(vJyLx);
		entity.setfJe(fJe.doubleValue());
		entity.setdJySj(now);
		entity.setdShSj(now);
		entity.setvBz(vBz);
		entity.setmName(mName);
		entity.setmType(mType);
		entity.setmNid(mNid);
		entity.setmBid(mBid);
		entity.setmGuid(mGuid);
		entity.setOperatorDept(operatorDept);
		entity.setOperatorPosition(operatorPosition);
		entity.setOperatorName(operatorName);
		entity.setOperatorUserId(operatorUserId);
		entity.setvZhLx(accountCountForCompany.getvZhLx());
		
		businessMapper.insertAccountDetailedForCompany(entity);
	}


	@Override
	public void insertAccountDetailedForCompany2(String vGsBm, String vGsBmP, String vJyLx, BigDecimal fJe, Date dJySj, Date dShSj, String vBz,
												 String mName, Integer mType, String mNid, String mBid, String mGuid, Date writeTime) throws Exception {
		OperateCompanyApply operateCompanyApply = operateCompanyApplyMapper.findVGsBm(vGsBm);
		OperateCompanyApply operateCompanyApplyP = operateCompanyApplyMapper.findVGsBm(vGsBmP);

		AccountCountForCompany accountCountForCompany = accountCountForCompanyMapper.findByVGsBm(vGsBmP);
		//Date now = new Date();
		AccountDetailedForCompany entity = new AccountDetailedForCompany();
		entity.setGuid(RandomValueUtils.getGUID());
		entity.setvGsBm(vGsBm);
		entity.setvGsJc(operateCompanyApply.getvGsJc());
		entity.setvGsMc(operateCompanyApply.getvGsMc());
		entity.setvYhZh(operateCompanyApply.getvYhZh());
		entity.setvGsBmP(vGsBmP);
		entity.setvGsJcP(operateCompanyApplyP.getvGsJc());
		entity.setvGsMcP(operateCompanyApplyP.getvGsMc());
		entity.setvYhZhP(operateCompanyApplyP.getvYhZh());
		entity.setvZhLx(accountCountForCompany.getvZhLx());

		entity.setvJyLx(vJyLx);//交易类型
		entity.setfJe(fJe.doubleValue());//交易金额
		entity.setdJySj(dJySj);//交易时间
		entity.setdShSj(dShSj);//审核时间
		entity.setvBz(vBz);//备注
		if(mName!=null)entity.setmName(mName);
		if(mType!=null)entity.setmType(mType);
		if(mNid!=null)entity.setmNid(mNid);
		if(mBid!=null)entity.setmBid(mBid);
		if(mGuid!=null)entity.setmGuid(mGuid);
		entity.setWriteTime(writeTime!=null?writeTime:dShSj);


		businessMapper.insertAccountDetailedForCompany(entity);
	}
}
